//
//  WJOperationDocumentView.swift
//  PDFSign
//
//  Created by 李永彬 on 2025/9/28.
//

import UIKit

class WJOperationDocumentView: UIView {
    
    lazy var iconImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.tintColor = .clear
        imageView.image = UIImage(named: "home_top_unsigned")
        return imageView
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .left
        label.font = UIFont.montserratMedium(16)
        label.text = "Document 2.pdf"
        label.textColor = UIColor(hex: "#121212")
        return label
    }()
    
    lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .left
        label.font = UIFont.montserratRegular(12)
        label.text = "pdf · 138 KB · July 17,2025"
        label.textColor = UIColor(hex: "#999999")
        return label
    }()
    
    lazy var contentView: UIView = {
        let view = UIView()
        view.layer.cornerRadius = 12.0
        view.layer.masksToBounds = true
        view.backgroundColor = UIColor(hex: "#FFFFFF")
        view.addSubview(iconImgView)
        return view
    }()
    
    var listModel: WJPDFListModel? = nil {
        didSet {
            if let data = listModel?.imageData {
                iconImgView.image = UIImage(data: data)
            }
            
            titleLabel.text = listModel?.name
            
            let suffix = listModel?.suffix ?? ""
            var size = ""
            if let fileSize = listModel?.fileSize {
                size = formatFileSize(bits: Double(fileSize))
            }
            var date = ""
            if let createdDate = listModel?.createdDate {
                date = formatTimestamp(createdDate.timeIntervalSince1970)
            }
            detailLabel.text = suffix + " · " + size + " · " + date
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(contentView)
        contentView.addSubview(iconImgView)
        contentView.addSubview(titleLabel)
        contentView.addSubview(detailLabel)
        commonIntSubviews()
    }
    
    func commonIntSubviews() {
        backgroundColor = .clear
        
        contentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        iconImgView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(12)
            make.width.height.equalTo(48)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(iconImgView)
            make.leading.equalTo(iconImgView.snp.trailing).offset(8)
            make.trailing.equalToSuperview().offset(-5)
            make.height.equalTo(20)
        }
        
        detailLabel.snp.makeConstraints { make in
            make.bottom.equalTo(iconImgView)
            make.leading.trailing.equalTo(titleLabel)
            make.height.equalTo(20)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
